package com.sunyard.manage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunyard.dal.dto.znsTask.TaskZNSDTO;
import com.sunyard.dal.entity.BoxAlarm;
import com.sunyard.dal.entity.Terminal;
import com.sunyard.dal.mapper.BoxAlarmMapper;
import com.sunyard.dal.mapper.TerminalMapper;
import com.sunyard.dal.mapper.ZNSTaskMapper;
import com.sunyard.manage.service.BoxAlarmService;
import com.sunyard.manage.service.TerminalService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author jimin.duan
 * @Description TODO
 * @createTime 2022年07月29日 14:14:00
 */
@Service
public class BoxAlarmImpl implements BoxAlarmService {
    @Autowired
    private TerminalMapper terminalMapper;
    @Autowired
    private ZNSTaskMapper znsTaskMapper;
    @Autowired
    private BoxAlarmMapper boxAlarmMapper;

    @Override
    public Object addAlarm(String longitude, String latitude, int lockStatus, int doorStatus, String numbers) {
        JSONObject jsonObject = new JSONObject();
        int status = 0;
        String msg = "不是报警数据";
        Terminal box = terminalMapper.queryTrunkSn(numbers);
        if (box != null) {
            if (lockStatus == 1 || doorStatus == 1) {
                String missionId = "";
                int alarmStatus = 1;
                if (doorStatus == 1) {
                    alarmStatus = 2;
                }
                if (doorStatus == 1 && lockStatus == 1) {
                    alarmStatus = 3;
                }
                BoxAlarm boxAlarm = new BoxAlarm();
                boolean alarm = false;
                List<TaskZNSDTO> missionList = znsTaskMapper.selectByBoxId(box.getTrunkSn());
                //0 在途
                if ("0".equals(box.getTaskStatus())) {
                    if (missionList.size() != 0) {
                        missionId = missionList.get(0).getTaskId();
                    }
                    boxAlarm.setDescription("任务中开箱预警");
                    alarm = true;
                }
                //在金库
//                if ("2".equals(box.getTaskStatus())) {
//                    boxAlarm.setDescription("非调拨任务开箱");
//                    alarm = true;
////                    for (TaskZNSDTO mission : missionList) {
////                        missionId = mission.getTaskId();
////                        if (mission.getType() == 3) {
////                            boxAlarm.setDescription("");
////                            alarm = false;
////                            break;
////                        }
////                    }
//                }
//                if ("1".equals(box.getStatus())) {
//                    Organization organization = organizationMapper.selectById(box.getOrganizationId());
//                    if (organization != null && organization.getFenceOpen() == 1 && !organization.getLatitude().equals("") && !organization.getLongitude().equals("")) {
//                        if (!latitude.equals("0.0") && !longitude.equals("0.0")) {
//                            GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(latitude), Double.parseDouble(longitude));
//                            GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(organization.getLatitude()), Double.parseDouble(organization.getLongitude()));
//                            double distance = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target).getEllipsoidalDistance();
//                            if (distance > organization.getRadius()) {
//                                alarm = true;
//                                boxAlarm.setDescription("未在网点范围内开箱");
//                            }
//                        }
//                    }
//                }
                if (alarm) {
//                    box.setAlarm(alarmStatus);
//                    boxMapper.updateById(box);
                    boxAlarm.setBoxId(box.getId());
                    if (!"0.0".equals(latitude) && !"0.0".equals(longitude)) {
                        boxAlarm.setLatitude(latitude);
                        boxAlarm.setLongitude(longitude);
                    }
                    boxAlarm.setCreateTime(System.currentTimeMillis() + "");
                    boxAlarm.setLockName("");
                    boxAlarm.setMissionId(Integer.parseInt(missionId));
                    boxAlarm.setOperateUser("");
                    boxAlarm.setLockStatus(lockStatus + "");
                    boxAlarm.setDoorStatus(doorStatus + "");
                    boxAlarm.setNumbers(numbers);
                    boxAlarm.setType("1");
                    boxAlarmMapper.insert(boxAlarm);
                    status = 1;
                    msg = "创建成功";
                }
            }
        } else {
            msg = "尾箱不存在";
        }
        jsonObject.put("msg", msg);
        jsonObject.put("status", status);
        return jsonObject;
    }

}
